package com.example.hcd.datamanage;

import java.math.BigDecimal;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ParseData {

	public ReimburseData getRiemburseDetail(String result) {
		System.out.println(result);
		ReimburseData data = new ReimburseData(null);
		try {
			JSONObject obj = new JSONObject(result);

			if (!obj.isNull("imgPrefix")) {
				data.setImgPrefix(obj.getString("imgPrefix"));
			}

			if (obj.isNull("oaBorrow")) {

				data.setBorrow(null);
			} else {
				JSONObject borrowinfo = obj.getJSONObject("oaBorrow");
				BorrowData borrow = new BorrowData(null);
				borrow.setID(borrowinfo.getString("id"));
				borrow.setProcessInstanceId(borrowinfo.getString("processInstanceId"));
				borrow.setTitle(borrowinfo.getString("title"));
				data.setBorrow(borrow);
			}

			// 获取报销子项目信息
			JSONArray projectDeatilArr = obj.getJSONArray("reimbursementWayDetail");
			for (int i = 0; i < projectDeatilArr.length(); i++) {
				ProjectChildData childData = new ProjectChildData();
				JSONObject obj1 = projectDeatilArr.getJSONObject(i);
				childData.info = obj1.getString("reimway");
				childData.numOfmoney = obj1.getDouble("reiamount");
				data.getReimburseUseinfoList().add(childData);
			}

			// 获取附件图片信息
			JSONArray reimImageArr = obj.getJSONArray("reimbursementImageList");
			for (int i = 0; i < reimImageArr.length(); i++) {
				ImageFileType info = new ImageFileType();
				JSONObject obj1 = reimImageArr.getJSONObject(i);
				info.setId(obj1.getString("id"));
//				info.setTaskId(obj1.getString("pid"));
				info.setImageFilePath(obj1.getString("imagePath"));
				info.setCreateTime(obj1.getLong("createtime"));
				info.setOp(obj1.isNull("op") ? null : obj1.getString("op"));
				data.getImageList().add(info);
			}

			// 获取报销内容
			JSONObject reimInfo = obj.getJSONObject("reimbursement");
			{
				data.setId(reimInfo.getString("id"));
				data.setProcessInstanceId(reimInfo.getString("processInstanceId"));
				data.setProcessDefinitionId(
						reimInfo.isNull("processDefinitionId") ? null : reimInfo.getString("processDefinitionId"));
				data.setTitle(reimInfo.getString("title"));
				data.setUserId(reimInfo.getString("userId"));
				data.setProjectName(reimInfo.getString("projectName"));
				data.setStatus(reimInfo.getLong("status"));
				data.setAmount(reimInfo.getDouble("amount"));
				data.setCreatetime(reimInfo.getLong("createtime"));
				data.setPayee(reimInfo.getString("payee"));
				data.setReimbursementMan(reimInfo.getString("reimbursementMan"));
				data.setUpdatetime(reimInfo.isNull("updatetime") ? 0 : reimInfo.getLong("updatetime"));
				data.setRemark(reimInfo.isNull("remark") ? "" : reimInfo.getString("remark"));
				data.setReason(reimInfo.isNull("reason") ? null : reimInfo.getString("reason"));
				data.setWorkflowStatus(reimInfo.getLong("workflowStatus"));
				data.setProcessStatus(reimInfo.getString("processStatus"));
				data.setProcessStatusValue(reimInfo.getString("processStatusValue"));
				data.setApplicant(reimInfo.getString("applicant"));
				data.setIshasReceipt(reimInfo.isNull("hasReceipt") ? null : reimInfo.getString("hasReceipt"));
				// BorrowData borrow = new BorrowData(null);
				// borrow.setProcessInstanceId(reimInfo.isNull("BorrowId")?null:reimInfo.getString("BorrowId"));
				// data.setBorrow(borrow);
			}

		} catch (JSONException e) {
			return null;
		}

		return data;
	}

	public PaymentData getPaymentDetail(String result, String key) {
		PaymentData data = new PaymentData(null);

		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(key);
			data.setId(myJs.getString("id"));
			data.setTitle(myJs.getString("title"));
			data.setActionTime(myJs.getString("actiontime"));
			data.setGatheringManName(myJs.getString("gatheringManName"));
			data.setGatheringManId(myJs.getString("gatheringManId"));
			data.setApplyPeopleId(myJs.getString("userid"));
			data.setBankName(myJs.getString("bankName"));
			data.setBankCardId(myJs.getString("bankCardId"));
			data.setReason(myJs.getString("reason"));
			data.setTaxNumber(myJs.getString("taxNumber"));
			data.setAmount(new BigDecimal(myJs.getString("amount")));
			data.setProcessId(myJs.getString("processInstanceId"));
			data.setFileName(myJs.getString("filename"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusId(myJs.getString("processStatusValue"));
			data.setType(myJs.isNull("type") ? "" : myJs.getString("type"));
			data.setTypeName(myJs.isNull("typeName") ? "" : myJs.getString("typeName"));
			data.setApplicant(myJs.getString("applicant"));
			data.setIshasReceipt(myJs.isNull("hasReceipt") ? null : myJs.getString("hasReceipt"));
			if (!obj.isNull("imgPrefix")) {
				data.setImgPrefix(obj.getString("imgPrefix"));
			}
			// 获取附件图片信息
			JSONArray payImageArr = obj.getJSONArray("files");
			for (int i = 0; i < payImageArr.length(); i++) {
				ImageFileType info = new ImageFileType();
				String imageName = payImageArr.get(i).toString();

				info.setImageFilePath(imageName);

				data.getImageList().add(info);
			}

		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public BorrowData getBorrowDetail(String result, String type) {
		BorrowData data = new BorrowData(null);

		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setTitle(myJs.getString("title"));
			data.setAmount(myJs.getString("amount"));
			data.setBorrowTime(myJs.getString("borrowTime"));
			data.setRemark(myJs.isNull("reason") ? "" : myJs.getString("reason"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setType(myJs.isNull("type") ? null : myJs.getString("type"));
			data.setBank(myJs.isNull("bankName") ? null : myJs.getString("bankName"));
			data.setNumofbank(myJs.isNull("bankCardld") ? null : myJs.getString("bankCardld"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setApplicant(myJs.getString("applicant"));

			data.setReimburSum(obj.getLong("reimburSum"));
			data.setDiffPrice(obj.getLong("diffPrice"));

			JSONArray jsonarr = obj.getJSONArray("reimbursementList");


			for (int i = 0; i < jsonarr.length(); i++) {
				JSONObject myJS = jsonarr.getJSONObject(i);
				ReimburseData reimburse = new ReimburseData(null);
				reimburse.setId(myJS.getString("id"));
				reimburse.setProcessInstanceId(myJS.getString("processInstanceId"));
				reimburse.setTitle(myJS.getString("title"));
				reimburse.setProjectName(myJS.getString("projectName"));
				reimburse.setStatus(myJS.getLong("status"));
				reimburse.setAmount(myJS.getDouble("amount"));
				reimburse.setRemark(myJS.getString("remark"));
				reimburse.setCreatetime(myJS.getLong("createtime"));
				reimburse.setWorkflowStatus(myJS.getLong("workflowStatus"));
				data.getReimburseList().add(reimburse);
			}

		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public PurchaseData getPurchaseDetail(String result, String type) {
		PurchaseData data = new PurchaseData(null);

		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setTitle(myJs.getString("title"));
			data.setAmount(myJs.isNull("amount") ? "0" : myJs.getString("amount"));
			data.setPurchaseTime(myJs.isNull("purchaseTime") ? "" : myJs.getString("purchaseTime"));
			data.setReason(myJs.isNull("reason") ? "" : myJs.getString("reason"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setPurchaseType(myJs.isNull("purchaseType") ? "" : myJs.getString("purchaseType"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setApplicant(myJs.getString("applicant"));
			data.setBankName(myJs.getString("bankName"));
			data.setSupplierName(myJs.getString("supplierName"));
			data.setBankCardId(myJs.getString("bankCardId"));
			data.setProjectName(myJs.getString("projectName"));
			data.setIshasReceipt(myJs.isNull("hasReceipt") ? null : myJs.getString("hasReceipt"));
			data.setUseSeal(myJs.getString("useSeal").equals("yes") ? "是" : "否");
			if (!obj.isNull("imgPrefix")) {
				data.setImgPrefix(obj.getString("imgPrefix"));
			}
			// 获取附件图片信息
			JSONArray payImageArr = obj.getJSONArray("files");
			for (int i = 0; i < payImageArr.length(); i++) {
				ImageFileType info = new ImageFileType();
				String imageName = payImageArr.get(i).toString();

				info.setImageFilePath(imageName);

				data.getImageList().add(info);
			}

		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public SealuseData getSealUserDetail(String result, String type) {
		SealuseData data = new SealuseData(null);

		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setId(myJs.getString("id"));
			data.setUserId(myJs.getString("userid"));
			data.setUserName(myJs.getString("userName"));
			data.setDeptName(myJs.getString("deptName"));
			data.setSealName(myJs.getString("sealType"));
			data.setEstimereturn(myJs.isNull("estimereturn") ? null : myJs.getString("estimereturn"));
			data.setRemark(myJs.isNull("remark") ? null : myJs.getString("remark"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setCreatetime(myJs.getString("createtime"));
			data.setReturnTime(myJs.isNull("retruntime") ? null : myJs.getString("retruntime"));
			data.setReceivetime(myJs.isNull("receivetime") ? null : myJs.getString("receivetime"));
			data.setIsLendingout(myJs.getString("lending"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));

		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public OverTimeData getOverTimeDetail(String result, String type) {
		OverTimeData data = new OverTimeData();
		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = null;
			myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setUserId(myJs.getString("userid"));
			data.setStartTime(myJs.getString("startDate"));
			data.setEndTime(myJs.getString("endDate"));
			data.setOvertimeReason(myJs.getString("reason"));
			data.setCreateTime(myJs.getString("createtime"));
			data.setStatus(myJs.getString("status"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setLeavestatus(myJs.getString("leavestatus"));
			data.setAllTime(myJs.getString("overtimes"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public OverTimeData getOffDetail(String result, String type) {
		OverTimeData data = new OverTimeData();
		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setUserId(myJs.getString("userid"));
			data.setStartTime(myJs.getString("startDate"));
			data.setEndTime(myJs.getString("endDate"));
			data.setOvertimeReason(myJs.getString("reason"));
			data.setCreateTime(myJs.getString("createtime"));
			data.setStatus(myJs.getString("status"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setLeavestatus(myJs.getString("leavestatus"));
			data.setAllTime(myJs.getString("overtimes"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public LeaveData getLeaveDetail(String result, String type) {
		LeaveData data = new LeaveData();
		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setUserId(myJs.getString("userid"));
			data.setStartTime(myJs.getString("startDate"));
			data.setEndTime(myJs.getString("endDate"));
			data.setReason(myJs.getString("reason"));
			data.setCreateTime(myJs.getString("createtime"));
			data.setStatus(myJs.getString("status"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setLeavestatus(myJs.getString("leavestatus"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));
			data.setLeaveType(myJs.getString("leaveType"));
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public OutData getOutDetail(String result, String type) {
		OutData data = new OutData();
		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setUserId(myJs.getString("userid"));
			data.setStartTime(myJs.getString("startDate"));
			data.setEndTime(myJs.getString("endDate"));
			data.setReason(myJs.getString("reason"));
			data.setCreateTime(myJs.getString("createtime"));
			data.setStatus(myJs.getString("status"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));
			data.setIscarout(myJs.getString("isUserCar"));
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}

	public BusniessTravelData getBusinessDetail(String result, String type) {
		BusniessTravelData data = new BusniessTravelData();
		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setID(myJs.getString("id"));
			data.setUserId(myJs.getString("userid"));
			data.setStartTime(myJs.getString("startDate"));
			data.setEndTime(myJs.getString("endDate"));
			data.setReason(myJs.getString("reason"));
			data.setCreateTime(myJs.getString("actiontime"));
			data.setStatus(myJs.getString("status"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));

			data.setToAddress(myJs.getString("toAddress"));
			data.setTranType(myJs.getString("tranType"));
			data.setProjectName(myJs.getString("projectName"));
			data.setTitle(myJs.getString("title"));

		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}


	public UseCarData getUseCarDetail(String result, String type) {
		UseCarData data = new UseCarData();
		try {
			JSONObject obj = new JSONObject(result);
			JSONObject myJs = obj.getJSONObject(type);
			data.setUserId(myJs.getString("userid"));
			data.setUserName(myJs.getString("username"));
			data.setStartData(myJs.getString("startDate"));
			data.setEndDate(myJs.getString("endDate"));
			data.setReason(myJs.getString("reason"));
			data.setDestination(myJs.getString("destination"));
			data.setPredictMileage(myJs.getInt("predictMileage"));
			data.setProcessInstanceId(myJs.getString("processInstanceId"));
			data.setProcessStatus(myJs.getString("processStatus"));
			data.setProcessStatusValue(myJs.getString("processStatusValue"));
			data.setApplicant(myJs.getString("applicant"));
			if (!obj.isNull("imgPrefix")) {
				data.setImgPrefix(obj.getString("imgPrefix"));
			}
			JSONArray payImageArr = obj.getJSONArray("aliossImageList");
			for (int i = 0; i < payImageArr.length(); i++) {
				ImageFileType info = new ImageFileType();
				String imageName = payImageArr.get(i).toString();

				info.setImageFilePath(imageName);

				data.getImageList().add(info);
			}
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

		return data;

	}


}
